﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.DataTransformationServices.Controls;
using System.ComponentModel;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Runtime.Design;
using PowerPivotUpdater;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace PowerPivotUpdaterUI
{
    public partial class GeneralView : UserControl, IDTSTaskUIView
    {
        private PropertyGrid propertyGrid;
        private GeneralViewNode generalNode;

        public GeneralView()
        {
            InitializeComponent();
        }

        #region IDTSTaskUIView Members

        public void OnCommit(object taskHost)
        {
            TaskHost host = taskHost as TaskHost;
            if (host == null)
            {
                throw new ArgumentException("Argument is not a TaskHost.", "taskHost");
            }

            UpdatePPTask task = host.InnerObject as UpdatePPTask;
            if (task == null)
            {
                throw new ArgumentException("Argument is not a Power Pivot Updater Task.", "taskHost");
            }

            host.Name = generalNode.Name;
            host.Description = generalNode.Description;

            // Task properties
            task.ExcelFileURL = generalNode.ExcelFile;
            task.Connection = generalNode.Connection;
            task.WebSite = generalNode.WebSiteURL;
        }

        public void OnInitialize(IDTSTaskUIHost treeHost, TreeNode viewNode, object taskHost, object connections)
        {
            this.generalNode = new GeneralViewNode(taskHost as TaskHost, connections as IDtsConnectionService);
            this.propertyGrid.SelectedObject = generalNode;
        }

        public void OnLoseSelection(ref bool bCanLeaveView, ref string reason)
        {
        }

        public void OnSelection()
        {
        }

        public void OnValidate(ref bool bViewIsValid, ref string reason)
        {
        }

        #endregion



        #region Designer code

        private void InitializeComponent()
        {
            this.propertyGrid = new System.Windows.Forms.PropertyGrid();
            this.SuspendLayout();
            // 
            // propertyGrid
            // 
            this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Fill;
            this.propertyGrid.Location = new System.Drawing.Point(0, 0);
            this.propertyGrid.Name = "propertyGrid";
            this.propertyGrid.PropertySort = System.Windows.Forms.PropertySort.Categorized;
            this.propertyGrid.Size = new System.Drawing.Size(150, 150);
            this.propertyGrid.TabIndex = 0;
            this.propertyGrid.ToolbarVisible = false;
            // 
            // GeneralView
            // 
            this.Controls.Add(this.propertyGrid);
            this.Name = "GeneralView";
            this.ResumeLayout(false);
        }

        #endregion
    }

    #region GeneralNode

    public class GeneralViewNode
    {
        // Properties variables
        private string excelFileURL = string.Empty;
        private string name = string.Empty;
        private string webSiteURL = string.Empty;
        private string connection = string.Empty;
        private string description = string.Empty;

        public GeneralViewNode(TaskHost taskHost, IDtsConnectionService connectionService)
        {
            // Extract common values from the Task Host
            name = taskHost.Name;
            description = taskHost.Description;

            // Extract values from the task object
            UpdatePPTask task = taskHost.InnerObject as UpdatePPTask;
            if (task == null)
            {
                string msg = string.Format(CultureInfo.CurrentCulture, "Type mismatch for taskHost inner object. Received: {0} Expected: {1}", taskHost.InnerObject.GetType().Name, typeof(UpdatePPTask).Name);
                throw new ArgumentException(msg);
            }

            excelFileURL = task.ExcelFileURL;
            webSiteURL = task.WebSite;
            connection = task.Connection;
        }

        #region Properties

        [Category("General"), Description("Task name")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                string v = value.Trim();
                if (string.IsNullOrEmpty(v))
                {
                    throw new ArgumentException("Task name cannot be empty");
                }
                name = v;
            }
        }

        [Category("General"), Description("Task description")]
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value.Trim();
            }
        }


        [Category("General"), Description("Excel File URL")]
        public string ExcelFile
        {
            get
            {
                return this.excelFileURL;
            }
            set
            {
                excelFileURL = value;
            }
        }

        [Category("General"), Description("Sharepoint Site"), TypeConverter(typeof(SiteSelector))]
        public string WebSiteURL
        {
            get
            {
                return webSiteURL;
            }
            set
            {
                webSiteURL = value;
            }
        }


        [Category("General"), Description("Connection"), TypeConverter(typeof(ConnectionSelector))]
        public string Connection
        {
            get
            {
                return connection;
            }
            set
            {
                connection = value;
            }
        }
        #endregion
    }

    #endregion
}
